﻿using Adalbertus.BudgetPlanner.Domain;
using Adalbertus.BudgetPlanner.Domain.Repositories;

namespace Adalbertus.BudgetPlanner.Services
{
    public class CashFlowService : BaseService<CashFlow>, ICashFlowService
    {
        public CashFlowService(IRepositoryFactory repositoryFactory, IUnitOfWorkProvider unitOfWorkProvider)
            : base(repositoryFactory, unitOfWorkProvider)
        {
        }

        public CashFlow Create(string name, string description = null)
        {
            using (IUnitOfWork unitOfWork = UnitOfWorkProvider.OpenUnitOfWork())
            {
                CashFlow cashFlow = new CashFlow
                {
                    Name = name,
                    Description = description
                };
                IRepository<CashFlow> cashFlowRepository = RepositoryFactory.CreateRepository<CashFlow>(unitOfWork);
                cashFlowRepository.Save(cashFlow);

                unitOfWork.Commit();

                return cashFlow;
            }
        }
    }
}
